﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.Core.Library;
using MP.LayerHelper.MP_OBS.BLL;
public partial class Member_Control_Login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadCombo();
        }
    }

    void loadCombo()
    {
        //DropDownList ddl = (DropDownList)Login1.FindControl("ddlBranch");
        //ddl.DataTextField = "BranhName";
        //ddl.DataValueField = "Id";
        //ddl.DataSource = HrBranchManager.CreateInstant().GetAllBranch();
        //ddl.DataBind();
        //ddl.SelectedValue = "c261d504-f356-4f33-b92f-21fc321d4ac5";
    }

    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        MP.Core.Library.Logger.CreateInstant("Login").Debug("Login thanh cong. Load lai cac gia tri trong config");
        
        //DropDownList ddl = (DropDownList)Login1.FindControl("ddlBranch");
        //if (ddl != null)
        //{
        //    //CookieManager.setCookie(Page, "cBranchId", ddl.SelectedValue);
        //}
        if (!string.IsNullOrEmpty(Request["ReturnUrl"]))
        {
            MP.Core.Library.Logger.CreateInstant("Member/Login").Debug("Login thanh cong. Load sysparam");
            Response.Redirect(Request["ReturnUrl"]);
        }
        else
            Response.Redirect("/");
        Response.Write("login thanh cong");
    }

    protected void Login1_LoginError(object sender, EventArgs e)
    {
        Login log = (Login)sender;
        string user = log.UserName;
        string pass = log.Password;

        if (Membership.FindUsersByName(user).Count <= 0)
            Login1.FailureText = "Tài khoản không tồn tại";
        else
        {
            MembershipUser u = Membership.GetUser(user);
            if (!Membership.ValidateUser(user, pass))
            {
                if (!u.IsApproved)
                    Login1.FailureText = "Tài khoản đã bị khóa";
                else
                    Login1.FailureText = "Mật khẩu không đúng";
            }
        }
    }
}
